package kr.jobseeker.movinavi;

import java.net.URLEncoder;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.os.StrictMode;
import android.util.Log;
public class HTMLparserForRest {
    protected RestaurantInfo parser(String Restaurant) throws Exception {
    	StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());

    	String restName;
    	String restAddr;
    	String url = new String();
    	ArrayList<String> rate = new ArrayList<String>();
    	ArrayList<String> review = new ArrayList<String>();
    	RestaurantInfo restaurant = new RestaurantInfo();
    	//가게이름 및 주소
    	restaurant.setRestaurant(Restaurant);
    	
    	//1차 url 생성
    	restName = Restaurant.substring(0, Restaurant.indexOf(","));
    	restAddr = Restaurant.substring(Restaurant.indexOf(",")+1, Restaurant.length()-1);
    	restName = URLEncoder.encode(restName, "UTF-8");
    	restAddr = URLEncoder.encode(restAddr, "UTF-8");
    	url = "https://plus.google.com/local/" + restAddr + "/s/" + restName;
    	StringBuilder builder = new StringBuilder();
        // get html document structure
        Document document = Jsoup.connect(url).get();
        // selector query
        Elements rows = document.select("h3.Amc");
        if(!rows.isEmpty()){
        	
        	//CID 얻어와 2차 URL 생성(모바일 주소)
        	Element row = rows.select("a").first();
        	String linkHref = row.attr("href");
        	linkHref = linkHref.substring(2,linkHref.length()-6);
        	String newURL = new String("https://plus.google.com/app/basic/local/" 
        								+ linkHref + "/about?source=apppromo");
        	Log.d("결과", newURL);
            Document document1 = Jsoup.connect(newURL).get();
            
            //tagSelector에 정보 받아와 해당 변수에 저장
            //가게 간략 정보
            Elements tagSelector = document1.select("div.ine");
            for (Element aTag : tagSelector.select("span.jMd")) {
            	builder.append(aTag.text()+", ");
            }
            restaurant.setInfo(builder.toString());
            //전화번호
            Element aTag = tagSelector.select("div.jne a").first();
            restaurant.setPhone(aTag.attr("href"));
            //rate평균
            tagSelector = document1.select("span.ola");
            if(!tagSelector.isEmpty()){
                restaurant.setRateAvg(tagSelector.first().text() + "/5.0");
            }else {
            	restaurant.setRateAvg("-/5.0");
            }

            //평가
            tagSelector = document1.select("div.rne");
            for (Element aTag2 : tagSelector.select("div.mMd")) {
            	String stars = new String();
            	for (int i=0; i<aTag2.select("span.b-db-ac.b-db-ac-th").size(); i++) {
            		stars += "★";
            	}
            	for (int i=0; i<(5-aTag2.select("span.b-db-ac.b-db-ac-th").size()); i++) {
            		stars += "☆";
            	}
            	rate.add(stars);
            	review.add(aTag2.select("div.tne").text());
            	stars = "";
            }
            restaurant.setRate(rate);
            restaurant.setReview(review);
        }else {
        	Log.d("결과", "암것두없으요");
        }
        return restaurant;
    }
    
}
